package com.gfxy.modules.educationandservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gfxy.modules.base.vo.TeacherBaseInfoVo;
import com.gfxy.modules.common.service.IGfxyCommonService;
import com.gfxy.modules.educationandservice.service.IEducationAndServiceService;
import com.gfxy.modules.system.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.SpringContextHolder;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: lijixin 2021/11/12 11:05 上午
 */

@Api(tags="育人与服务")
@RestController
@RequestMapping("/educationandservice/educationAndServiceImport")
@Slf4j
public class EducationAndServiceImportController {

    @Autowired
    private IGfxyCommonService gfxyCommonService;
    @Autowired
    private IEducationAndServiceService educationAndServiceService;


    @RequestMapping(value = "/exportXlsTemplate")
    public ModelAndView exportXls(
            @RequestParam(name="orgCode") String orgCode,
            HttpServletRequest request)  {

        try{
            List<TeacherBaseInfoVo> sysUsers = gfxyCommonService.getTeachingTeacherByOrgCode(orgCode);


            List<String> title = new ArrayList<>();
            title.add("工号");
            title.add("姓名");
            title.add("工作内容");
            title.add("积分");

            List<List<String>> contents = new ArrayList<>();
            for (TeacherBaseInfoVo sysUser : sysUsers) {
                List<String> content = new ArrayList<>();
                content.add(sysUser.getId());
                content.add(sysUser.getRealname());
                content.add("");
                content.add("");
                contents.add(content);
            }


            EducationAndServiceExcelView educationAndServiceExcelView = SpringContextHolder.getBean(EducationAndServiceExcelView.class);
            ModelAndView mv = new ModelAndView(educationAndServiceExcelView);
            mv.addObject(EducationAndServiceExcelView.FILE_NAME,"育人与服务模板");
            mv.addObject(EducationAndServiceExcelView.TITLE, title);
            mv.addObject(EducationAndServiceExcelView.CONTENT, contents);
            return mv;
        } catch (Exception e){
            throw e;
        }
    }

    @AutoLog(value = "育人与服务-分页列表查询")
    @ApiOperation(value="育人与服务-分页列表查询", notes="育人与服务-分页列表查询")
    @GetMapping(value = "/listScore")
    public Result<?> queryPageList(@RequestParam(name = "date") String date,
                                   @RequestParam(name = "orgCode") String orgCode,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        Page<Map> resultPage = new Page<>(pageNo, pageSize);
        Page<Map> resultPage1 = resultPage.setRecords(educationAndServiceService.queryTotalScoreByOrgCodeAndDate(date, orgCode));
        return Result.OK(resultPage1);
    }

    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            MultipartFile file = entity.getValue();// 获取上传文件对象
            ImportParams params = new ImportParams();
            params.setTitleRows(2);
            params.setHeadRows(1);
            params.setNeedSave(true);
            try {

                return Result.OK(educationAndServiceService.importExcel(file));
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                return Result.error("文件导入失败:" + e.getMessage());
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Result.OK("文件导入失败！");
    }


}
